home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / runtime architecture / component manager / delegateonlycomponent / mycomponent.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.6 KB  |  217 lines

  1. /*
  2.     File:        MyComponent.c
  3.  
  4.     Contains:    simple component sample.
  5.  
  6.     Written by: John Wang    
  7.  
  8.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #ifdef THINK_C
  25. #define        applec
  26. #endif
  27.  
  28. #include    <Memory.h>
  29. #include    <Errors.h>
  30. #include    <Components.h>
  31. #include    <Movies.h>
  32. #include    <QuickTimeComponents.h>
  33. #include    <Components.h>
  34. #include    <LowMem.h>
  35.  
  36. #include    "MyComponent.h"
  37.  
  38. /* ------------------------------------------------------------------------- */
  39.  
  40. //    Component entry point.
  41.  
  42. pascal ComponentResult main(ComponentParameters *params, char **storage)
  43. {
  44.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  45.     long                ret;
  46.     
  47.     if ( kDEBUGME )
  48.         DebugStr("\pIn main()");
  49.     
  50.     if ( params->what < 0 ) { 
  51.         switch ( params->what ) {
  52.             case kComponentOpenSelect:
  53.                 return ( CallComponentFunction(params, (ComponentFunctionUPP) MyOpen) );
  54.     
  55.             case kComponentCloseSelect:
  56.                 return ( CallComponentFunctionWithStorage(storage, params,
  57.                         (ComponentFunctionUPP) MyClose) );
  58.                 
  59.             case kComponentCanDoSelect:
  60.                 ret = CallComponentFunction(params, (ComponentFunctionUPP) MyCanDo);
  61.                 if ( ret == false ) {
  62.                     DebugStr("\pIn kComponentCanDoSelect");
  63.                     if ( (**myPrivateGlobals).delegate ) {
  64.                         ret = DelegateComponentCall(params, (**myPrivateGlobals).delegate);
  65.                     }
  66.                 }
  67.                 return ( ret );
  68.     
  69.             case kComponentVersionSelect: 
  70.                 return ( CallComponentFunction(params, (ComponentFunctionUPP) MyVersion) );
  71.     
  72.             case kComponentRegisterSelect: 
  73.                 return ( CallComponentFunctionWithStorage(storage, params,
  74.                         (ComponentFunctionUPP) MyRegister) );
  75.  
  76.             case kComponentTargetSelect: 
  77.                 return ( CallComponentFunctionWithStorage(storage, params,
  78.                         (ComponentFunctionUPP) MyTarget) );
  79.  
  80.             default:
  81.                 return ( paramErr );
  82.         }
  83.     } else {
  84.         switch ( params->what ) {
  85.             default:
  86.                 if ( (**myPrivateGlobals).delegate ) {
  87.                     //    If base media handler is targeted, then delegate all unimplemented
  88.                     //    calls to the base media handler.
  89.                     long    ret;
  90.                     
  91.                     ret = DelegateComponentCall(params, (**myPrivateGlobals).delegate);
  92.                     return ( ret );
  93.                 } else {
  94.                     //    If base media handler has not been targeted, then return paramErr.
  95.                     return ( paramErr );
  96.                 }
  97.         }
  98.     }
  99. }
  100.  
  101. /* ------------------------------------------------------------------------- */
  102.  
  103. //    Required component calls.
  104.  
  105. pascal ComponentResult MyOpen(ComponentInstance self)
  106. {
  107.     PrivateGlobals             **myPrivateGlobals;
  108.     ComponentInstance        myComp;
  109.     ComponentDescription    searchComp;
  110.     Component                dataHandler;
  111.     
  112.     if ( kDEBUGME )
  113.         DebugStr("\pIn MyOpen()");
  114.         
  115.     myPrivateGlobals = nil;
  116.     searchComp.componentType = 'dhlr';
  117.     searchComp.componentSubType = 'alis';
  118.     searchComp.componentManufacturer = 'appl';
  119.     searchComp.componentFlags = 0x40000000;
  120.     searchComp.componentFlagsMask = 0x40000000;
  121.  
  122.     //    Open base media handler component and target it.
  123.     dataHandler = FindNextComponent(nil, &searchComp);
  124.     if (dataHandler == nil) {
  125.         DebugStr("\pCould not find data handler.");
  126.     }
  127.     if ((myComp = OpenComponent(dataHandler)) == nil) {
  128.         return(componentNotCaptured);
  129.     }
  130.     ComponentSetTarget(myComp, self);
  131.     
  132.     //    Create private variables.
  133.     myPrivateGlobals = (PrivateGlobals **) NewHandleClear(sizeof(PrivateGlobals));
  134.     if ( myPrivateGlobals == nil )
  135.         goto bail;
  136.     
  137.     //    Initialize private variables.
  138.     (**myPrivateGlobals).delegate = myComp;
  139.  
  140.     //    Since we've gotten here, everyt hings ok and we can set up the connection.
  141.     SetComponentInstanceStorage(self, (Handle) myPrivateGlobals);
  142.     return ( noErr );
  143.  
  144. bail:
  145.     if ( myPrivateGlobals )
  146.         DisposeHandle((Handle) myPrivateGlobals);
  147.     return ( memFullErr );
  148. }
  149.  
  150. pascal ComponentResult MyClose(Handle storage, ComponentInstance self)
  151. {
  152.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  153.  
  154.     if ( kDEBUGME )
  155.         DebugStr("\pIn MyClose()");
  156.         
  157.     //    Dispose of private variables.
  158.     if ( myPrivateGlobals ) {
  159.         if ((**myPrivateGlobals).delegate) {
  160.             CloseComponent((**myPrivateGlobals).delegate);
  161.             (**myPrivateGlobals).delegate = nil;
  162.         }
  163.         DisposeHandle((Handle) myPrivateGlobals);
  164.     }
  165.     
  166.     return ( noErr );
  167. }
  168.  
  169. pascal ComponentResult MyCanDo(short selector)
  170. {    
  171.  
  172.     if ( kDEBUGME )
  173.         DebugStr("\pIn MyCanDo()");
  174.     
  175.     switch ( selector ) {
  176.     
  177.         //    Required component calls.
  178.         case kComponentOpenSelect:
  179.         case kComponentCloseSelect:
  180.         case kComponentCanDoSelect:
  181.         case kComponentVersionSelect: 
  182.         case kComponentRegisterSelect: 
  183.         case kComponentTargetSelect: 
  184.  
  185.         //    MyComponent specific calls.
  186.  
  187.         //    Not handled.
  188.         default:
  189.             return ( false );
  190.     }
  191. }
  192.  
  193. pascal ComponentResult MyVersion()
  194. {
  195.     if ( kDEBUGME )
  196.         DebugStr("\pIn MyVersion()");
  197.  
  198.     return ( (kMyComponentSpec<<16) | (kMyComponentVersion) );
  199. }
  200.  
  201. pascal ComponentResult MyRegister()
  202. {
  203.     return ( false );
  204. }
  205.  
  206. pascal ComponentResult MyTarget(Handle storage, ComponentInstance self)
  207. {
  208.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  209.  
  210.     if ( kDEBUGME )
  211.         DebugStr("\pIn MyTarget()");
  212.  
  213.     //    From now on, self will be the component instance that targeted us.
  214.     (**myPrivateGlobals).self = self;
  215.     
  216.     return ( noErr );
  217. }